<template>
	<view class="gradens-yhrw-zxviewupdate">
		<view class="gradens-yhrw-view-card">
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name">计划名称:</view>
				<text class="yhrw-view-list-text">{{planInfo.planName}}</text>
			</view>
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name">养护类型:</view>
				<text class="yhrw-view-list-text">{{planInfo.maintainType}}</text>
			</view>
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name">起止时间:</view>
				<text class="yhrw-view-list-text">{{planInfo.startTime | date('yyyy/mm/dd')}} ~ {{planInfo.endTime | date('yyyy/mm/dd')}}</text>
			</view>
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name">养护单位:</view>
				<text class="yhrw-view-list-text">{{planInfo.unit}}</text>
			</view>
			<view class="yhrw-view-list-item">
				<view class="yhrw-view-list-name">专项内容:</view>
				<text class="yhrw-view-list-text"><dict-tag :options="dicts.relevance_type_name"
						:value="planInfo.relevanceTypeName" /></text>
			</view>
			<view class="" style="border-bottom: 1px solid #c5c5c5;margin-top: 20rpx;"></view>
			<view style="margin-top: 20rpx;">
				<uni-forms ref="form" label-position="top" :label-width="75" :modelValue="formData" :rules="rules">
					<uni-forms-item label="填报内容" name="maintainPlanContent">
						<uni-easyinput type="textarea" v-model="formData.maintainPlanContent" placeholder="请输入填报内容" />
					</uni-forms-item>
				</uni-forms>
			</view>
			<view class="" style="display: flex;position: absolute;bottom: 20rpx;width: 100%;">
				<button type="primary" style="width: 200rpx;" @click="goImage">上传图片</button>
				<button type="primary" style="width: 200rpx;" @click="submit">提交</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		listMaintainSpecialFill,
		addMaintainSpecialFill
	} from '@/api/comp/maintainSpecialFill'
	import dictTag from "@/components/dict-tag"
	import {
		getToken
	} from '@/utils/auth'
	import {
		baseUrl
	} from "@/config.js";
	import {
		listMaintainSpecialPlan,
		getMaintainSpecialPlan,
		updateMaintainSpecialPlan,
	} from "@/api/comp/maintainSpecialPlan";
	import {
		mapState
	} from "vuex";
	export default {
		dicts: ['relevance_type_name'],
		components: {
			dictTag
		},
		data() {
			return {
				date: new Date(),
				// showCalendar: false,

				formData: {
					maintainPlanContent: '',
				},
				planInfo: {},
				rules: {
					maintainPlanContent: {
						rules: [{
							required: true,
							errorMessage: '不能为空',
						}]
					},
				}
			}
		},
		onLoad(options) {
			this.getInfo(options)
		},
		computed: {
			...mapState({
				name: (state) => state.user.name,
				userInfo: (state) => state.user.userInfo,

			}),
		},
		methods: {
			goImage() {
				uni.navigateTo({
					url: '/pages/gardens/yhrw/zximage'
				})
			},
			async getInfo(options) {
				let planlist = await getMaintainSpecialPlan(options.id)
				this.planInfo = planlist.data
			},
			uploadfn(tempFilePaths) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: baseUrl + '/am/amAttach/uploadAndSaveTheFile', //仅为示例，非真实的接口地址
						filePath: tempFilePaths,
						name: 'file',
						header: {
							Authorization: 'Bearer ' + getToken()
						},
						formData: {
							mainId: this.planInfo.id,
							bussType: 'td_comp_maintain_special_fill'
						},
						success: (uploadFileRes) => {
							console.log(uploadFileRes.data);
							resolve(uploadFileRes.data)
						},

						fail: function(err) {
							reject(err)
						}
					});
				})
			},
			backClick() {
				uni.navigateBack()
			},
			generateRandomNumber() {
				const min = 10000; // 最小值（包含）
				const max = 99999; // 最大值（包含）
				return Math.floor(Math.random() * (max - min + 1)) + min + '';
			},
			submit() {
				this.$refs.form.validate().then(res => {
					this.planInfo.maintainPlanContent = res.maintainPlanContent
					this.planInfo.remark = '已填报'
					this.planInfo.rptTime = new Date().getTime();
					this.planInfo.userNames = this.name;
					this.planInfo.planId = this.planInfo.id;
					let imglist = this.$store.state.yhrw.planImgList
					let arr = imglist.map(file => {
						return this.uploadfn(file)
					})
					Promise.all(arr).then(_ => {
						updateMaintainSpecialPlan(this.planInfo).then(_ => {
							this.$u.toast('提交成功')
						}).catch(_ => {
							this.$u.toast('提交失败')
						})
						this.planInfo.id = new Date().getTime() + this.generateRandomNumber()
						addMaintainSpecialFill(this.planInfo).then(res => {
							setTimeout(() => {
								uni.navigateBack()
							}, 2000)
						})
					})
				}).catch(err => {
					// console.log('表单错误信息：', err);
					// this.$u.toast('查询条件为空，查询错误')
				})
			}

		}
	}
</script>

<style lang="scss">
	$nav-height: 30px;

	.box-bg {
		background-color: #F5F5F5;
		padding: 5px 0;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		// width: 160rpx;
		margin-left: 4px;
	}

	.input-uni-icon {
		line-height: $nav-height;
	}

	.gradens-yhrw-view-card {
		box-sizing: border-box;
		padding: 10rpx 10px;
	}

	.gradens-yhrw-zxviewupdate {
		.yhrw-view-list-item {
			display: flex;
			padding-top: 20rpx;
			align-items: center;

			.yhrw-view-list-name {
				width: 125rpx;
			}

			.yhrw-view-list-text {
				flex: 1;
			}
		}
	}
</style>